<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>生产登记</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../css/public.css" media="all">
		<style>
			.layui-layer-page {
			padding: 10px !important;
		}
	</style>
	</head>

	<body>
		<div class="layuimini-container">
			<div class="layuimini-main">
				<!--搜索开始-->
				<fieldset class="layui-elem-field layuimini-search">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="" id="searchFrm">
							<div class="layui-form-item">
								<!-- <div class="layui-inline">
									<label class="layui-form-label">查询时间</label>
									<div class="layui-input-inline">
										<div class="layui-input-inline">
											<input type="text" name="registerTime" id="register_time" readonly="readonly" autocomplete="off" class="layui-input">
										</div>
									</div>
								</div> -->
								<div class="layui-inline">
									<label class="layui-form-label">产品名称</label>
									<div class="layui-input-inline">
										<div class="layui-input-inline">
											<input type="text" name="product_name" id="product_name" class="layui-input">
										</div>
									</div>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-block" style="text-align: center;">
									<a class="layui-btn " lay-submit="" lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
									<a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" lay-filter="data-search-btn"><label
										 class="layui-icon layui-icon-refresh"></label>重置</a>
								</div>
							</div>
						</form>
					</div>
				</fieldset>
				<!--搜索结束-->

				<!--表格开始-->
				<table class="layui-hide" id="MassageTable" lay-filter="MassageTable"></table>
				<!-- <div style="display: none;" id="PlanToolBar">
					<a class="layui-btn layui-btn-sm inport_btn_add" lay-event="add">添加</a>
				</div> -->
				<div style="display: none;" id="MassageTableBar">
					<a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_Processes" lay-event="Processes">登记</a>
					<!-- <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_update" lay-event="update">修改</a>
					<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete inport_btn_delete" lay-event="delete">删除</a>
					<a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_back" lay-event="back">退货</a> -->
				</div>
				<!--表格结束-->

				<!-- 详情数据 -->
				<div id="SeeProcesses" style="display: none;padding: 0.3125rem;">
					<!--表格开始-->
					<table class="layui-hide" id="ProcessesTable" lay-filter="ProcessesTable"></table>
					<!-- <div style="display: none;" id="ProcessesToolBar">
						<a class="layui-btn layui-btn-sm inport_btn_add" lay-event="add">添加</a>
					</div> -->
					<div style="display: none;" id="ProcessesTableBar">
						<a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_ProcessesMaterial" lay-event="ProcessesMaterial">登记</a>
						<!-- <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_update" lay-event="update">修改</a>
						<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete inport_btn_delete" lay-event="delete">删除</a>
						<a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete inport_btn_back" lay-event="back">退货</a> -->
					</div>
				</div>

				<div id="SeeProcessesMaterial" style="display: none;padding: 1.3125rem 3.3125rem 0.3125rem 3.3125rem;">
					<form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm2" id="dataFrm2">
						<div class="layui-col-md12 layui-col-xs12">
							<div class="layui-row ">
								<div class="layui-inline">

									<!-- <input type="hidden" name="id" /> -->

									<div class="layui-form-item magt3">
										<label class="layui-form-label">工序名称</label>
										<div class="layui-input-block">
											<input type="text" name="procedureName" disabled="disabled" id="procedureName" class="layui-input"
											 placeholder="工序名称">
											<input type="hidden" name="id" id="idp" class="layui-input">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">设计工时</label>
										<div class="layui-input-block">
											<input type="text" name="labourHourAmount" id="labourHourAmount" disabled="disabled" class="layui-input"
											 placeholder="设计工时">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">已使用工时</label>
										<div class="layui-input-block">
											<input type="text" name="realLabourHourAmount" id="realLabourHourAmount" disabled="disabled" class="layui-input"
											 placeholder="0" value="0">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">本次工时数</label>
										<div class="layui-input-block">
											<input type="text" name="hour" id="hour" class="layui-input" placeholder="请输入本次工时数">
										</div>
									</div>
								</div>

								<!-- lay-verify="required" -->
								<!--表格开始-->
								<table class="layui-hide" id="ProcessesMaterialTable" lay-filter="ProcessesMaterialTable"></table>
								<div style="display: none;" id="AmountTableBar">
									<input type="text" class="bcRenewAmount layui-input" name="tags[]" placeholder="请输入本次使用数量" />
								</div>


								<div class="layui-form-item">
									<div class="" style="text-align: center;padding-top: 20px;">
										<a class="layui-btn " lay-submit="" lay-filter="register"><label class="layui-icon layui-icon-release"></label>提交</a>
										<!-- <a class="layui-btn layui-btn-warm" href="javascript:(AssignWorkOrderForProduction.html);" lay-filter="data-search-btn"><label
														class="layui-icon layui-icon-refresh"></label>返回上一级</a> -->
									</div>
								</div>
							</div>
					</form>
				</div>
			</div>
		</div>
		<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
		<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
		<script src="../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
		<script src="../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
		<script src="../../lib/common/common.js" charset="utf-8"></script>

		<script>
			layui.use(['form', 'table', 'laydate', 'layer'], function() {
				var form = layui.form;
				var table = layui.table;
				var laydate = layui.laydate;
				var layer = layui.layer;

				//渲染时间选择器
				laydate.render({
					elem: '#register_time',
					type: 'datetime'
				});
				// laydate.render({
				// 	elem: '#endTime',
				// 	type: 'datetime'
				// });

				// //加载查询条件里面的供应商的下拉列表
				// $.get(api6 + "provider/getAllAvailableProvider", function(res) {
				// 	var search_providerid = $(".providerSel");
				// 	var html = "<option value =''>请选择供应商</option>";
				// 	var data = res.data;
				// 	$.each(data, function(index, item) {
				// 		// 注入供应商ID 和 名称
				// 		html += "<option value =" + item.id + ">" + item.providername + "</option>"
				// 	});
				// 	search_providerid.html(html);
				// 	// 重载下拉框
				// 	form.render("select");
				// });


				// //加载查询条件里面的商品的下拉列表
				// $.get(api6 + "goods/getAllAvailableGoods", function(res) {
				// 	var search_goodsid = $(".goodsSel");
				// 	var html = "<option value =''>请选择商品</option>";
				// 	var data = res.data;
				// 	$.each(data, function(index, item) {
				// 		// 注入商品ID 和 名称
				// 		html += "<option value =" + item.id + ">" + item.goodsname + "--[" + item.size + "]" + "</option>"
				// 	});
				// 	search_goodsid.html(html);
				// 	// 重载下拉框
				// 	form.render("select");
				// });


				var tableIns = table.render({
					elem: '#MassageTable',
					url: api6 + 'massage/selAllMassage',
					toolbar: "#MassageToolBar",
					cellMinWidth: true,
					cols: [
						[{
								field: 'manufactureId',
								title: '派工单编号',
								align: "center",
								width: 250
							},
							{
								field: 'productId',
								title: '产品编号',
								align: "center",
								width: 150
							},
							{
								field: 'productName',
								title: '产品名称',
								align: "center",
								width: 150
							},
							{
								field: 'amount',
								title: '投产数量',
								align: "center",
								width: 133
							},
							{
								field: 'moduleCostPriceSum',
								title: '设计物料总成本',
								align: "center",
								width: 150
							},
							{
								field: 'labourCostPriceSum',
								title: '设计工时总成本',
								align: "center",
								width: 150
							},
							{
								title: '操作',
								templet: '#MassageTableBar',
								fixed: "right",
								align: "center",
								width: 180
							}
						]
					],
					page: true
				});

				// 监听搜索操作
				form.on('submit(data-search-btn)', function(data) {
					var params = $("#searchFrm").serialize();
					console.log(params);
					//执行搜索重载
					table.reload('MassageTable', {
						page: {
							curr: 1
						},
						url: api6 + 'massage/selAllMassage?' + params
					});
					return false;
				});

				//监听表头的事件
				table.on('toolbar(MassageTable)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'add':
							openAddInport();
							break;
					};
				});

				//监听添加弹出层里面的 供应商   的下拉改变事件
				form.on("select(data_providerid)", function(obj) {
					// 获取供应商ID
					var providerid = obj.value;
					// 获取商品下拉框
					var goodsDataSel = $(".goodsDataSel");
					initGoodsSeleted(providerid, goodsDataSel)
				})

				// 初始化下拉框 两级联动
				/**
				 *  providerid  供应商id
				 *  elem		商品下拉框
				 *  fun			回调函数
				 */
				function initGoodsSeleted(providerid, elem, fun) {
					var html = "<option value =''>请选择商品</option>";
					if (providerid != "") {
						// 根据供应商ID 查询商品
						$.get(api6 + "goods/getGoodsByProviderId", {
							providerid: providerid
						}, function(res) {
							var data = res.data;

							$.each(data, function(i, item) {
								// 进行追加数据
								html += "<option value =" + item.id + ">" + item.goodsname + "[" + item.size + "]" + "</option>"
							});

							// 覆盖到 elem  html内容当中
							elem.html(html);

							// 重载下拉框
							form.render("select");

							// 进行回调数据    
							if (fun != undefined) {
								fun();
							}
						});
					} else {
						elem.html(html);
						form.render("select");
					}
				}


				//监听行数据的事件
				table.on('tool(MassageTable)', function(obj) {
					var data = obj.data;
					if (obj.event === 'delete') {
						layer.confirm('真的删除这条进货数据么', function(index) {
							$.post(api6 + "inport/deleteInport", {
								id: data.id
							}, function(res) {
								layer.msg(res.msg);
								obj.del();
								layer.close(index);
							})
						});
					} else if (obj.event === 'update') {
						openUpdateInport(data);
					} else if (obj.event === 'back') {
						openBack(data);
					}else if (obj.event === 'Processes') {
						SeeProcesses(data.id);
					}
				});
				
				//监听行数据的事件
				table.on('tool(ProcessesTable)', function(obj) {
					var data = obj.data;
					if (obj.event === 'ProcessesMaterial') {
						SeeProcessesMaterial(data);
					 }
				});
				
				function SeeProcessesMaterial(data) {
					console.log(data);
					console.log($(".bcRenewAmount"));
					$("#procedureName").val(data.procedureName);
					$("#labourHourAmount").val(data.labourHourAmount);
					$("#realLabourHourAmount").val(data.realLabourHourAmount);
					$("#idp").val(data.id);
					mainIndex = layer.open({
						type: 1,
						title: '生产登记',
						content: $("#SeeProcessesMaterial"),
						area: ['1000px', '457px'],
						success: function(index) {
							// $.get(api6 + "massage/selPMaterial?id="+data.id, function(res) {
							// 		// console.log(res);
							// 		// form.val("plandata", res);
							// })
							var tableIns = table.render({
								elem: '#ProcessesMaterialTable',
								url: api6 + 'massage/selPMaterial?id='+data.id,
								toolbar: "#ProcessesMaterialToolBar",
								cellMinWidth: true,
								cols: [
									[{
											field: 'id',
											title: '序号',
											align: "center",
											width: 140
										},
										{
											field: 'productName',
											title: '物料名称',
											align: "center",
											width: 140
										},
										{
											field: 'productId',
											title: '物料编号',
											align: "center",
											width: 150
										},
										{
											field: 'amount',
											title: '设计数量',
											align: "center",
											width: 133
										},
										{
											field: 'renewAmount',
											title: '已使用数量',
											align: "center",
											width: 140
										},
										{
											field: 'countRen',
											title: '本次使用数量',
											// templet: '#AmountTableBar',
											edit:"text",
											fixed: "right",
											align: "center",
											width: 180
										}
									]
								]
							});
						}
					});
				}


















	//存储每一行的数据
	let dataPro = [];
	
	// 当前输入的数值

  //监听单元格编辑
  table.on('edit(ProcessesMaterialTable)', function(obj){
    var value = obj.value //得到修改后的值
    ,data = obj.data //得到所在行所有键值
    ,field = obj.field; //得到字段
	// console.log(data);
	console.log(data);
	let a = obj.value; // 1
	
	if (data !== undefined) {
	// 1    2   --- 【1，2】
		for (var i = 0; i < dataPro.length; i++) {
			let id = dataPro[i].id;
			if (id == data.id) {
				console.log("aaaaaaaaaaaaaaaaaaaaaaaaaa", dataPro[i])
				dataPro.splice(i, 1);
			}
		}
	
		dataPro.push(data);
		
	}
	console.log(dataPro);
  });
  
  //修改
  form.on("submit(register)", function(data) {
  	var params="";
  	var idp=$("#idp").val();
  	var hourp=$("#hour").val();
	params += "idp=" + idp;
	params += "&hourp=" + hourp;
	params += "&tregister=" + username;
	
	// params += "&processesMaterials=" + dataPro;





$.ajax({
						url: api6+"massage/updPM?"+params,
						type: 'POST',
						data: JSON.stringify(dataPro),
						dataType: 'json',
						contentType: 'application/json;charset=utf-8',
						success: function(data) {
							
							layer.msg(data.msg);
							if (data.code == 200) {
								tableIns.reload();
							}
							layer.close(mainIndex);
						},
						error: function(data) {
							console.log(data.status);
						}
					});
  
  	return false;
  });
  
  
			
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				form.on("submit(close)", function(data) {
					// 序列化
				// 	var params = $("#backDataFrm").serialize();
				
				// 	$.post(api6 + "outport/addOutport", params, function(res) {
				// 		layer.msg(res.msg);
				// 		if (res.code == 200) {
				// 			tableIns.reload();
				// 		}
						layer.close(mainIndex);
				// 	})
				// 	return false;
				});

// 修改进货 
				function SeeProcesses(data) {
					console.log(data);
					$("#id").val(data.id);
					mainIndex = layer.open({
						type: 1,
						title: '生产登记子页面',
						content: $("#SeeProcesses"),
						area: ['1000px', '457px'],
						success: function(index) {
							var tableIns = table.render({
								elem: '#ProcessesTable',
								url: api6 + 'massage/selProcesses?parentId='+data,
								toolbar: "#ProcessesToolBar",
								cellMinWidth: true,
								cols: [
									[{
											field: 'id',
											title: 'id',
											align: "center",
											width: 150
										},
										{
											field: 'procedureName',
											title: '工序名称',
											align: "center",
											width: 150
										},
										{
											field: 'labourHourAmount',
											title: '设计工时数',
											align: "center",
											width: 150
										},
										{
											field: 'realLabourHourAmount',
											title: '实际工时数',
											align: "center",
											templet:"#sjrealLabourHourAmount",
											width: 150
										},
										{
											field: 'subtotal',
											title: '设计工时成本',
											align: "center",
											width: 133
										},
										{
											field: 'realSubtotal',
											title: '实际工时成本',
											align: "center",
											templet:"#realSubtotal",
											width: 150
										},
										{
											field: 'moduleSubtotal',
											title: '设计物料成本',
											align: "center",
											width: 150
										},
										{
											field: 'realModuleSubtotal',
											title: '实际物料成本',
											align: "center",
											templet:"#realModuleSubtotal",
											width: 150
										},
										{
											field: 'procedureFinishTag',
											title: '登记状态',
											align: "center",
											templet:"#procedureFinishTag",
											width: 133
										},
										{
											field: 'procedureTransferTag',
											title: '交接状态',
											align: "center",
											templet:"#procedureTransferTag",
											width: 150
										},
										{
											title: '操作',
											templet: '#ProcessesTableBar',
											fixed: "right",
											align: "center",
											width: 180
										}
									]
								]
							});
						}
					});
				}

				
				
				// 修改进货 
				function openUpdateInport(data) {
					mainIndex = layer.open({
						type: 1,
						title: '修改[' + data.goodsname + ']进货',
						content: $("#addOrUpdateDiv"),
						area: ['675px', '407px'],
						success: function(index) {


							layer.tips('需要修改进货数量直接覆盖数量即可', '#numbers', {
								tips: 1
							});

							url = api6 + "inport/updateInport";
							var providerid = data.providerid;
							var goodsDataSel = $(".goodsDataSel");
							// 调用初始化下拉框 
							initGoodsSeleted(providerid, goodsDataSel, function() {
								//装载数据表格
								form.val("dataFrm", data);
							});
							// 禁用下拉框  
							$("#data_providerid").attr("disabled", true);
							$("#data_goodsid").attr("disabled", true);
							form.render();



						}
					});
				}
				//提交数据
				form.on("submit(doSubmit)", function(data) {
					var params = $("#dataFrm").serialize();
					$.post(url, params, function(res) {
						layer.msg(res.msg);
						if (res.code == 200) {
							tableIns.reload();
						}
						layer.close(mainIndex);
					})
					return false;
				});

				//打开退货的弹出层
				function openBack(data) {
					mainIndex = layer.open({
						type: 1,
						title: "【" + data.goodsname + '】的退货',
						content: $("#backDiv"),
						area: ['435px', '300px'],
						success: function(index) {
							//重置表单
							$("#dataFrm")[0].reset();
							// 装载数据
							form.val("backDataFrm", data);
							form.val("backDataFrm", {
								number: '',
								inportid: data.id
							});
						}
					});
				}

				//提交数据
				form.on("submit(doBack)", function(data) {
					// 序列化
					var params = $("#backDataFrm").serialize();

					$.post(api6 + "outport/addOutport", params, function(res) {
						layer.msg(res.msg);
						if (res.code == 200) {
							tableIns.reload();
						}
						layer.close(mainIndex);
					})
					return false;
				});

				form.verify({
					checkgoodsnumber: function(value, item) { //value：表单的值、item：表单的DOM对象
						var str;
						var goodsid = $("#back_goodsid").val();
						$.ajax({
							//  获取商品的数量  
							url: api6 + "goods/getGoodsByGoodId?goodsid=" + goodsid,
							type: 'get',
							async: false,
							success: function(res) {
								//  判断 商品 的数量 是否大于我要 退货的  数量  
								// 大于则违反逻辑
								if (res.data.number < value) {
									str = "退货数量不能大于当前商品库存";
								}
							}
						})
						return str;
					}
				});
			});
		</script>

		<script type="text/html" id="TcheckTag">
			{{# if(d.checkTag=='S001-1'){d.checkTag='等待审核'}}
						{{d.checkTag}}
					{{# }else if(d.checkTag=='S001-2'){d.checkTag='审核通过'}}
						{{d.checkTag}}
					{{# }else{d.checkTag='审核不通过'}}
						{{d.checkTag}}
					{{# }}}
		</script>
		<script type="text/html" id="ManufactureTag">
			{{# if(d.checkTag=='P001-1'){d.checkTag='未派工'}}
						{{d.checkTag}}
					{{# }else{d.checkTag='已派工'}}
						{{d.checkTag}}
					{{# }}}
		</script>
		<script type="text/html" id="realModuleSubtotal">
			{{# if(d.realModuleSubtotal==null){d.realModuleSubtotal='还未动工'}}
		 				{{d.realModuleSubtotal}}
		 			{{# }else{d.realModuleSubtotal=d.realModuleSubtotal}}
		 				{{d.realModuleSubtotal}}
		 			{{# }}}
		 </script>
		<script type="text/html" id="realSubtotal">
			{{# if(d.realSubtotal==null){d.realSubtotal='还未动工'}}
		 				{{d.realSubtotal}}
		 			{{# }else{d.realSubtotal=d.realSubtotal}}
		 				{{d.realSubtotal}}
		 			{{# }}}
		 </script>
		<script type="text/html" id="sjrealLabourHourAmount">
			{{# if(d.sjrealLabourHourAmount==null){d.sjrealLabourHourAmount='还未动工'}}
						{{d.sjrealLabourHourAmount}}
					{{# }else{d.sjrealLabourHourAmount=d.sjrealLabourHourAmount}}
						{{d.sjrealLabourHourAmount}}
					{{# }}}
		</script>
		<script type="text/html" id="procedureFinishTag">
			{{# if(d.procedureFinishTag=="G004-0"){d.procedureFinishTag='未开始'}}
						{{d.procedureFinishTag}}
					{{# }else if(d.procedureFinishTag=='G004-1'){d.procedureFinishTag='已完成'}}
						{{d.procedureFinishTag}}
					{{# }else if(d.procedureFinishTag=='G004-2'){d.procedureFinishTag='未完成'}}
						{{d.procedureFinishTag}}
					{{# }else{d.procedureFinishTag='已审核'}}
						{{d.procedureFinishTag}}
					{{# }}}
		</script>
		<script type="text/html" id="procedureTransferTag">
			{{# if(d.procedureTransferTag=="G005-0"){d.procedureTransferTag='未交接'}}
						{{d.procedureTransferTag}}
					{{# }else if(d.procedureTransferTag=='G005-1'){d.procedureTransferTag='已交接'}}
						{{d.procedureTransferTag}}
					{{# }else{d.procedureTransferTag='已审核'}}
						{{d.procedureTransferTag}}
					{{# }}}
		</script>
	</body>

</html>
